#!/bin/bash
#
# Copyright 2014 CyberVision, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

args_size=$#

root_log_dir=$1

owner=$2

function create_root_log_dir()
{
    if [ $(id -u) -eq 0 ]; then
        if [ $args_size -eq 2 ]; then
                if [ ! -d "$root_log_dir" ]; then
                    egrep "^$owner" /etc/passwd >/dev/null
                        if [ $? -eq 0 ]; then
                            sudo mkdir $root_log_dir
                            [ $? -eq 0 ] && echo "Root log directory has been created!" || echo "Failed to create root log directory!"
                            sudo chown $owner:$owner $root_log_dir
                            sudo chmod 755 $root_log_dir
                        else
                            echo "User doesn't exists. Can't create directory."
                        fi
                else
                    echo "Root log directory already exists!"
                fi
        else
            echo "Expected 2 arguments but was $args_size"
            exit 2
        fi
    else
        echo "You need root privileges to run this script"
        exit 1
    fi
}

create_root_log_dir